package com.echo.im.platform.controller;


import com.echo.im.common.result.Result;
import com.echo.im.domain.user.vo.LoginVO;
import com.echo.im.domain.user.service.UserService;
import com.echo.im.domain.user.dto.LoginDTO;
import com.echo.im.domain.user.dto.ModifyPwdDTO;
import com.echo.im.domain.user.dto.RefreshTokenDTO;
import com.echo.im.domain.user.dto.RegisterDTO;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author lxy
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登陆", description = "用户登陆")
    public Result<LoginVO> login(@Valid @RequestBody LoginDTO dto) {
        LoginVO vo = userService.login(dto);
        return Result.success(vo);
    }

    @PutMapping("/refreshToken")
    @Operation(summary = "刷新token", description = "用refreshtoken换取新的token")
    public Result refreshToken(@Valid @RequestBody RefreshTokenDTO dto) {
        LoginVO vo = userService.refreshToken(dto);
        return Result.success(vo);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册")
    public Result register(@Valid @RequestBody RegisterDTO dto) {
        userService.register(dto);
        return Result.success();
    }

    @PutMapping("/modifyPwd")
    @Operation(summary = "修改密码", description = "修改用户密码")
    public Result modifyPassword(@Valid @RequestBody ModifyPwdDTO dto) {
        userService.modifyPassword(dto);
        return Result.success();
    }

    @DeleteMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出")
    public Result logout() {
        userService.logout();
        return Result.success();
    }

}
